package com.yc.demo.d0224;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "NameItemsServlet", value = "/NameItemsServlet")
public class NameItemsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String name = request.getParameter("name");
        List<String> list = new ArrayList<>();
        for (String n : NameServlet.names){
            //判断 n 是否是 names开头
            if (n.toLowerCase().startsWith(name.toLowerCase())) {
                list.add(n);
            }
        }
        Result result = new Result(1,null,list);
        Gson gson = new Gson();
        final String json = gson.toJson(result);
        response.getWriter().println(json);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
